#include "film.h"
#include "options.h"
#include "bdd.h"
#include "editionfilm.h"
#include "QEventLoop"
#include "apiallocine.h"
#include "httpimage.h"

film::film(QObject *parent)
    :QObject(parent)
{
}
film::film(const QString & titre, QObject *parent)
    :QObject(parent)
{
    this->titreFilm = titre;
}

film::film(film &f)
    :QObject(f.parent())
{
}

film::~film()
{
}

film &film::operator =(const film &f )
{
    //titreFilm=f.titre();
    return *this;
}

//Méthodes

int film::Existe(int codeAllocine)
{
    options opt;
    bdd db = bdd(opt.nomBdd());
    QString requete = "SELECT idFilm FROM film WHERE codeAllocineFilm ="
            + db.TexteVersSql(QString::number(codeAllocine));
    return db.EstDansLaBase(requete);
}

void film::Enregistre()
{
    options opt;
    bdd db = bdd(opt.nomBdd());
    QString requete;

    // Si le film n'existe pas on le crée
    if (this->Existe(this->codeAllocineFilm) == -1)
    {
        requete = "INSERT INTO film(idFilm,codeAllocineFilm,titreFilm,titreOriginalFilm,"
                "synopsisFilm,codeAllocineBandeAnnonce,typeFilm,dateRealisationFilm,noteUtilisateurAllocineFilm,"
                "notePresseFilm,maNoteFilm,maCritiqueFilm,cheminAfficheFilm) VALUES(NULL, "
                + db.TexteVersSql(QString::number(this->codeAllocineFilm))+ ", "
                + db.TexteVersSql(this->titreFilm) + ", " + db.TexteVersSql(this->titreOriginalFilm) + ", "
                + db.TexteVersSql(this->synopsisFilm)+ ", " + db.TexteVersSql(QString::number(this->codeAllocineBandeAnnonceFilm)) + ", "
                + db.TexteVersSql(this->typeFilm)+ ", " + db.TexteVersSql(this->dateFilm) + ", " + db.TexteVersSql(QString::number(this->noteAllocineFilm))+ ", "
                + db.TexteVersSql(QString::number(this->notePresseFilm)) + ", " + db.TexteVersSql(QString::number(this->maNoteFilm))+ ", "
                + db.TexteVersSql(this->critiqueFilm) + ", " + db.TexteVersSql(this->afficheFilm) + ")";
    }
    // Si il y a plusieurs films trouvés
    else if (this->Existe(this->codeAllocineFilm) == -2)
    {
        QMessageBox msg(QMessageBox::Warning,"Erreur lors de l'enregistrement d'un film",
                        "Plusieurs films avec le même code allociné ont été trouvés dans la base !");
        msg.exec();
    }
    // Sinon on le modifie
    else
    {
        requete = "UPDATE film SET "
                "codeAllocineFilm = " + db.TexteVersSql(QString::number(this->codeAllocineFilm))+ ", "
                "titreFilm = " + db.TexteVersSql(this->titreFilm) + ", "
                "titreOriginalFilm = " + db.TexteVersSql(this->titreOriginalFilm) + ", "
                "synopsisFilm = " + db.TexteVersSql(this->synopsisFilm)+ ", "
                "codeAllocineBandeAnnonce = " + db.TexteVersSql(QString::number(this->codeAllocineBandeAnnonceFilm)) + ", "
                "typeFilm = " + db.TexteVersSql(this->typeFilm)+ ", "
                "dateRealisationFilm = " + db.TexteVersSql(this->dateFilm) + ", "
                "noteUtilisateurAllocineFilm = " + db.TexteVersSql(QString::number(this->noteAllocineFilm))+ ", "
                "notePresseFilm = " + db.TexteVersSql(QString::number(this->notePresseFilm)) + ", "
                "maNoteFilm = " + db.TexteVersSql(QString::number(this->maNoteFilm))+ ", "
                "maCritiqueFilm = " + db.TexteVersSql(this->critiqueFilm) + ", "
                "cheminAfficheFilm = " + db.TexteVersSql(this->afficheFilm) +
                "WHERE idFilm = " + db.TexteVersSql(QString::number(this->idFilm));
    }

    db.Execute(requete);
}

void film::Supprime()
{
    options opt;
    bdd db = bdd(opt.nomBdd());
    QString requete = "DELETE FROM film WHERE idFilm="+db.TexteVersSql(QString::number(this->idFilm));
    db.Execute(requete);
}

QList<film *> film::ListeFilms()
{
    options opt;
    bdd db = bdd(opt.nomBdd());
    QString requete = "SELECT idFilm,codeAllocineFilm,titreFilm,titreOriginalFilm,"
            "synopsisFilm,codeAllocineBandeAnnonce,typeFilm,dateRealisationFilm,noteUtilisateurAllocineFilm,"
            "notePresseFilm,maNoteFilm,maCritiqueFilm,cheminAfficheFilm FROM film ORDER BY titreFilm";
    db.Execute(requete);

    QList<film *> listeFilms;

    film* unFilm;

    while(db.Requete().next())
    {
        unFilm = new film((db.Requete().value(2).toString()));
        unFilm->idFilm = db.Requete().value(0).toInt();
        unFilm->codeAllocineFilm = db.Requete().value(1).toInt();
        unFilm->titreOriginalFilm = db.Requete().value(3).toString();
        unFilm->synopsisFilm = db.Requete().value(4).toString();
        unFilm->bandeAnnonceFilm = db.Requete().value(5).toString();
        unFilm->typeFilm = db.Requete().value(6).toString();
        unFilm->dateFilm = db.Requete().value(7).toString();
        unFilm->noteAllocineFilm = db.Requete().value(8).toFloat();
        unFilm->notePresseFilm = db.Requete().value(9).toFloat();
        unFilm->maNoteFilm = db.Requete().value(10).toFloat();
        unFilm->critiqueFilm = db.Requete().value(11).toString();
        unFilm->afficheFilm = db.Requete().value(12).toString();

        listeFilms << unFilm;
    }
    return listeFilms;
}

void film::NouvelId()
{
    options opt;
    bdd db = bdd(opt.nomBdd());
    QString requete = "SELECT MAX(idFilm) FROM film";
    db.Execute(requete);
    db.Requete().next();

    this->idFilm = db.Requete().value(0).toInt() + 1;
}

// Accesseurs

int film::id()
{
    return this->idFilm;
}

void film::setCodeAllocine(int code) // != id : code Allocine
{
    this->codeAllocineFilm = code;
}

int film::codeAllocine()
{
    return this->codeAllocineFilm;
}

void film::setCodeAllocineBandeAnnonce(int code)
{
    this->codeAllocineBandeAnnonceFilm=code;
}

int film::codeAllocineBandeAnnonce()
{
    return this->codeAllocineBandeAnnonceFilm;
}

void film::setDuree(int seconde)
{
    dureeSeconde = seconde;
}

int film::duree()
{
    return dureeSeconde;
}

void film::setTitre(QString titre)
{
    this->titreFilm = titre;
}

QString film::titre()
{
    return this->titreFilm;
}

void film::setTitreOriginal(QString titreOriginal)
{
    this->titreOriginalFilm = titreOriginal;
}

QString film::titreOriginal()
{
    return this->titreOriginalFilm;
}

void film::setSynopsis(QString synopsis)
{
    this->synopsisFilm = synopsis;
}

QString film::synopsis()
{
    return this->synopsisFilm;
}

void film::setCritique(QString critique)
{
    this->critiqueFilm = critique;
}

QString film::critique()
{
    return this->critiqueFilm;
}

void film::setAffiche(QString cheminAffiche)
{
    this->afficheFilm = cheminAffiche;
}

QString film::affiche()
{
    return this->afficheFilm;
}

void film::setBandeAnnonce(QString cheminBandeAnnonce)
{
    this->bandeAnnonceFilm = cheminBandeAnnonce;
}

QString film::bandeAnnonce()
{
    return this->bandeAnnonceFilm;
}

void film::setType(QString type)
{
    this->typeFilm = type;
}

QString film::type()
{
    return this->typeFilm;
}

void film::setNoteAllocine(float noteAllocine)
{
    this->noteAllocineFilm = noteAllocine;
}

float film::noteAllocine()
{
    return this->noteAllocineFilm;
}

void film::setNotePresse(float notePresse)
{
    this->notePresseFilm = notePresse;
}

float film::notePresse()
{
    return this->notePresseFilm;
}

void film::setMaNote(float maNote)
{
    this->maNoteFilm = maNote;
}

float film::maNote()
{
    return this->maNoteFilm;
}

void film::setDate(QString date)
{
    this->dateFilm = date;
}

QString film::date()
{
    return this->dateFilm;
}

void film::setListeRealisateurs(QList<personne> listeRealisateurs)
{
    this->listeRealisateursFilm = listeRealisateurs;
}

QList<personne> film::listeRealisateurs()
{
    return this->listeRealisateursFilm;
}

void film::setListeActeurs(QList<personne> listeActeurs)
{
    this->listeActeursFilm = listeActeurs;
}

QList<personne> film::listeActeurs()
{
    return this->listeActeursFilm;
}

void film::setListeNationalites(QList<nationalite> listeNationalites)
{
    this->listeNationalitesFilm = listeNationalites;
}

QList<nationalite> film::listeNationalites()
{
    return this->listeNationalitesFilm;
}

void film::setListeGenres(QList<genre> listeGenres)
{
    this->listeGenresFilm = listeGenres;
}

QList<genre> film::listeGenres()
{
    return this->listeGenresFilm;
}

void film::setPochette(QPixmap image)
{
    pochette = image;
}

QPixmap film::Pochette()
{
    return pochette;
}

void film::afficherFilm(QWidget *w)
{
    EditionFilm* edt = new EditionFilm(w);
    edt->lireFilm(this);
    edt->setVisible(true);
    connect(w,SIGNAL(destroyed()),edt,SLOT(deleteLater()));
}

void film::afficherFilmDialog()
{
    QDialog* dlg = new QDialog();
//    EditionFilm* edt = new EditionFilm(dlg);
//    edt->lireFilm(this);
//    edt->setVisible(true);
    connect(dlg,SIGNAL(finished(int)),dlg,SLOT(deleteLater()));
    afficherFilm(dlg);
    dlg->exec();
}

void film::parserTout()
{
    apiAllocine* api = new apiAllocine(this);
    httpImage* http = new httpImage();
    http->setFilm(this);
    this->connect(api, SIGNAL(FilmChoisi()), api, SLOT(InfoFilmParCode()));
    this->connect(api, SIGNAL(FilmParsee()), http, SLOT(recupererImageFilm()));
    this->connect(http, SIGNAL(fini()), http, SLOT(deleteLater()));
    this->connect(http, SIGNAL(fini()), api, SLOT(deleteLater()));
    this->connect(http, SIGNAL(fini()), this, SIGNAL(film_parse()));
    //this->connect(http, SIGNAL(fini()), this, SLOT(afficherFilmDialog()));
    api->ListeFilmParTitre();
}

void film::parserToutSynchrone()
{
    apiAllocine api(this);
    QEventLoop eventLoop;

    this->connect(&api, SIGNAL(FilmChoisi()), &eventLoop, SLOT(quit()));
    api.ListeFilmParTitre();
    eventLoop.exec();

    this->connect(&api,SIGNAL(FilmParsee()),&eventLoop, SLOT(quit()));
    api.InfoFilmParCode();
    eventLoop.exec();

    if(!this->affiche().isEmpty())
    {
        httpImage http;
        this->connect(&http,SIGNAL(fini()),&eventLoop, SLOT(quit()));
        http.imageDansPixmap(&pochette,this->affiche());
        eventLoop.exec();
    }
    //this->afficherFilmDialog();
}

void film::parserCodeParTitre()
{
}

void film::parserInfoParCode()
{
}

void film::parserAffiche()
{
}

void film::parserBandeAnnonce()
{
}

void film::enregistrement()
{
    this->Enregistre();
}
